package com.hollycrm.hollysqm.handle.service.factory;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hollycrm.hollysqm.entities.TblQmPlan;
import com.hollycrm.hollysqm.entities.TblQmPlanLog;
import com.hollycrm.hollysqm.entities.TblQmPlanParam;
import com.hollycrm.hollysqm.handle.service.AllotMethodService;
import com.hollycrm.hollysqm.handle.service.ExtractMethodService;
import com.hollycrm.hollysqm.handle.service.PaperFilterService;
import com.hollycrm.hollysqm.handle.service.PlanChain;
import com.hollycrm.hollysqm.handle.service.PlanService;

/**
 * 执行质检计划抽取动作
 * @author jianglong
 * @date 2017年4月1日 上午11:16:15
 */
@Service
public class PlanChainManager {

	private static final Logger log = LoggerFactory.getLogger(PlanChainManager.class);

	private int testCout= 0;
	public void setTestCout(int testCout){
		this.testCout = testCout;
	}
	public void getTestCout(String name){
		System.out.println(name +"-----------"+testCout);
	}
	
	@Autowired
	private PlanService planService;
	@Autowired
	private PlanChainFactory planChainFactory;
	@Autowired
	private PaperFilterService paperFilterService;
	@Autowired
	private AllotMethodService allotMethodService;
	@Autowired
	private ExtractMethodService extractMethodService;
	
	
	
	/**
	 * 执行计划抽取，根据质检计划的类型判断是执行V8或I8
	 * @param planId
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String excute(String planId) throws Exception {
		TblQmPlan plan = planService.getQmPlan(planId);
		if (plan == null){
			log.error("数据库中未查到当前质检计划，计划ID【"+planId+"】...");
			return "fial_"+System.currentTimeMillis();
		} else if (!StringUtils.equals(plan.getIsValid(), "1")) {
			log.error("当前质检计划无效，是否有效标识【"+plan.getIsValid()+"】,计划ID【"+planId+"】...");
			return "fial_"+System.currentTimeMillis();
		} else if (!StringUtils.equals(plan.getStatus(), "1")) {
			log.error("当前质检计划未启用，状态标识【"+plan.getStatus()+"】,计划ID【"+planId+"】...");
			return "fial_"+System.currentTimeMillis();
		}
		TblQmPlanLog planLog = planService.getQmPlanLog(planId);
		if (planLog!=null){
			log.info("当前质检计划正在执行中，不能同时启动多个相同计划，计划ID【"+planId+"】...");
			return "ok_"+System.currentTimeMillis();
		}
		TblQmPlanParam planParam = planService.getQmPlanParam(planId);
		String paramJosn = planParam.getParamJosn();
		if (StringUtils.isBlank(paramJosn)){
			throw new RuntimeException("计划的paramJosn参数为空...");
		}
		log.info("计划名称【"+plan.getPlanName()+"】,计划ID【"+planId+"】...");
		log.info("计划参数【"+ paramJosn +"】");
		planLog = planService.saveQmPlanLog(planId, plan.getModifier());//记录日志		
		//json转map
		ObjectMapper objMapper = new ObjectMapper();
		Map<String,Object> paramMap = null;
		int paperCount = 0;		
		try{
			paramMap = objMapper.readValue(paramJosn, Map.class);
			this.exceuteRule(plan, planParam, paramMap);
			log.info("【"+planId+"】计划分配质检单...");
			paperCount = allotMethodService.excuteAllot(plan , planParam, paramMap, plan.getDataType());//按策略分配质检单给每一个质检员
			planService.updateParamJosn(planParam, paramMap);
		}catch(Exception e){
			throw e;
		}finally{
			log.info("当前【"+planId+"】计划共抽取入库【"+ paperCount +"】条");
			planLog.setPaperCount(paperCount);//记录抽取的量
			planService.updateQmPlanLog(planLog);//更新日志
			try{
				planService.deletePlan(planId);//清理抽取的数据
			}catch(Exception e){
				throw e;
			}
		}
		return "ok_"+System.currentTimeMillis();
	}
	
	/**
	 * 循环抽取每个坐席人员的接触记录信息
	 * @param planId
	 * @param paramMap
	 * @throws Exception
	 */
	public void exceuteRule(TblQmPlan plan,TblQmPlanParam planParam,Map<String,Object> paramMap) throws Exception {
		int count = 0;
		PlanChain planChain = planChainFactory.getPlanChain(plan.getDataType());
		List<String> agentList = extractMethodService.getPlanAgentList(plan.getPlanId());
		for (String agentCode : agentList){
			log.info("【"+ plan.getPlanId() +"】计划 抽取工号【"+agentCode+"】接触记录信息...");
			paramMap.put("agentCode", agentCode);
			planChain.applyRule(planParam, paramMap);//数据过滤			
			count = paperFilterService.filterUser(plan, agentCode);
			log.info("【"+ plan.getPlanId() +"】计划 重复质检单过滤【"+ count +"】条...");
			log.info("【"+ plan.getPlanId() +"】计划 执行抽取策略...");
			extractMethodService.excuteExtract(planParam, paramMap, plan.getDataType());//按策略抽取每个坐席一定数量的质检单
			paramMap.remove("agentCode");
		}
	}
	
}
